﻿using System;
using System.Web.UI;



namespace DotNetNuke.Modules.UserDefinedTable
{


  public class EditCreatedAt : EditControl
  {
    private string viewStateName;

    public EditCreatedAt()
    {
      //base.Init += new EventHandler(this.EditCreatedAt_Init);
      //base.Load += new EventHandler(this.Page_Load);
    }



    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      //base.Load += new EventHandler(this.Page_Load);
      //base.Init += new EventHandler(this.EditCreatedAt_Init);
      this.viewStateName = string.Format("UDT-ECA{0}{1}", this.FieldID.ToString(), this.FieldType);
    }




    //protected void Page_Load(object sender, EventArgs evt)
    protected override void OnLoad(EventArgs evt)
    {
      base.OnLoad(evt);

      if ( this.Page.IsPostBack && !string.IsNullOrEmpty((string)this.ViewState[this.viewStateName]) )
        this.Controls.Add(new LiteralControl(string.Format("<span class=\"Normal\">{0}</span>", ((string)this.ViewState[this.viewStateName]))));
      
    }




    //private void EditCreatedAt_Init(object sender, EventArgs evt)
    //{
    //  this.viewStateName = string.Format("UDT-ECA{0}{1}", this.FieldID.ToString(), this.FieldType);
    //}



    public override string Value
    {
      get
      {
        if (this.ViewState[this.viewStateName] == null)
          return DateTime.Now.ToString("s");

        return DateTime.Parse( (string)this.ViewState[this.viewStateName] ).ToString("s");
      }
      set
      {
        if ( !string.IsNullOrEmpty(value) )
        {
          this.ViewState[this.viewStateName] = value;
          this.Controls.Add(new LiteralControl(string.Format("<span class=\"Normal\">{0:g}</span>", DateTime.Parse(value))));
        }
      }
    }


  }

}

